package com.night.icm.service.base.service.comprehensive_test;

import com.night.icm.model.base.comprehensive_test.StylisticPointDTO;
import com.night.icm.persistence.base.entity.comprehensive_test.ComprehensiveScore;
import com.night.icm.persistence.base.entity.comprehensive_test.StylisticPoint;
import com.night.icm.persistence.base.repository.comprehensive_test.StylisticPointRepository;
import com.night.icm.persistence.business.entity.SystemFile;
import com.night.icm.persistence.business.reporsitory.SystemFileRepository;
import com.night.icm.service.base.mapper.comprehensive_test.StylisticPointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * StylisticPointServiceImpl
 *
 * @author Liao ZhiYong
 * @date 2020/3/10
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class StylisticPointServiceImpl implements StylisticPointService {

    private final SystemFileRepository systemFileRepository;
    private final StylisticPointMapper stylisticPointMapper;
    private final StylisticPointRepository stylisticPointRepository;

    @Autowired
    public StylisticPointServiceImpl(SystemFileRepository systemFileRepository, StylisticPointMapper stylisticPointMapper, StylisticPointRepository stylisticPointRepository) {
        this.systemFileRepository = systemFileRepository;
        this.stylisticPointMapper = stylisticPointMapper;
        this.stylisticPointRepository = stylisticPointRepository;
    }


    /**
     * 新建
     *
     * @param stylisticPointDTO  文体分
     * @param comprehensiveScore 关联的综合分
     * @return 文体分
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StylisticPoint create(StylisticPointDTO stylisticPointDTO, ComprehensiveScore comprehensiveScore) {
        StylisticPoint stylisticPoint = this.stylisticPointMapper.toEntity(stylisticPointDTO);
        this.stylisticPointRepository.save(stylisticPoint);
        // 保存文体分的附件，并关联文体分附件
        List<String> fileIds = stylisticPointDTO.getFileIds();
        saveFile(stylisticPoint, fileIds);
        return stylisticPoint;
    }

    /**
     * 更新文体分
     *
     * @param stylisticPointDTO  文体分
     * @param comprehensiveScore 关联的综合分
     * @return 文体分
     */
    @Override
    public StylisticPoint update(StylisticPointDTO stylisticPointDTO, ComprehensiveScore comprehensiveScore) {
        // 更新学业分
        Optional<StylisticPoint> byId = this.stylisticPointRepository.findById(comprehensiveScore.getStylisticPoint().getId());
        StylisticPoint stylisticPoint = byId.get();
        this.stylisticPointMapper.updateEntity(stylisticPointDTO, stylisticPoint);
        // 删除原有附件
        stylisticPoint.setFiles(null);
        this.stylisticPointRepository.save(stylisticPoint);
        // 添加新的附件
        List<String> fileIds = stylisticPointDTO.getFileIds();
        saveFile(stylisticPoint, fileIds);
        this.stylisticPointRepository.save(stylisticPoint);
        return stylisticPoint;
    }

    /**
     * 保存文件
     *
     * @param stylisticPoint 对象
     * @param fileIds        文件id数组
     */
    private void saveFile(StylisticPoint stylisticPoint, List<String> fileIds) {
        if (null != fileIds && !fileIds.isEmpty()) {
            List<Long> ids = new ArrayList<>();
            fileIds.forEach(id -> ids.add(Long.parseLong(id)));
            List<SystemFile> files = this.systemFileRepository.findAllById(ids);
            stylisticPoint.setFiles(files);
        }
    }
}